import React, { Component } from 'react';
import { Modal, Input, Row, Col, Select, Form, message } from 'antd';
import moment from 'moment';
import 'moment/locale/zh-cn';

moment.locale('zh-cn');

const FormItem = Form.Item;
const Option = Select.Option;

function toString(str, type) {
  if (typeof str !== 'undefined' && str !== null) return str.toString();
  if (type === 'SELECT') return undefined;
  return '';
}

class UpdateBuySiteModel extends Component {

  constructor(props) {
    super(props);
    this.state = {
    };
  }

  handleSubmit() {
    const { form, modalValues } = this.props;
    form.validateFieldsAndScroll((err, values) => {
      if (err) return;
      if (modalValues) {
        this.props.dispatch({
          type: 'purchase/updateAllSkuBuySite',
          payload: { ...values, oldBuySite:modalValues },
        });
        this.closeModal();
      } else {
      	message.error('老采购站点不能为空');
      }
    });
  }

  closeModal() {
    const { form, close } = this.props;
    form.resetFields();
    close();
  }

  render() {
    const p = this;
    const { form, visible, modalValues = {}} = this.props;
    const { getFieldDecorator } = form;

    // 详情数据
    const agencyData = modalValues || {};

    const modalProps = {
      visible,
      wrapClassName: 'modalStyle',
      title: '修改',
      maskClosable: false,
      closable: true,
      onOk() {
        p.handleSubmit();
      },
      onCancel() {
        p.closeModal();
      },
    };
    const formItemLayout = {
      labelCol: { span: 8 },
      wrapperCol: { span: 10 },
    };

    return (
      <Modal
        {...modalProps}
      >
        <Form onSubmit={this.handleSubmit.bind(this)}>
          <Row>
            <Col>
              <FormItem
                label="采购站点"
                {...formItemLayout}
              >
                {getFieldDecorator('buySite', {
                  initialValue: toString(agencyData),
                  rules: [{ required: false, message: '请输入新的采购站点' }],
                })(
                  <Input placeholder="请输入新的采购站点" />,
                )}
              </FormItem>
            </Col>
          </Row>
        </Form>
      </Modal>
    );
  }
}

export default Form.create()(UpdateBuySiteModel);
